home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4473 < prev    next >
Encoding:
Text File  |  1996-08-05  |  5.9 KB  |  295 lines

  1. Path: sn.no!usenet
  2. From: jan-henrik.haukeland@fou.telenor.no (jan-henrik haukeland)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Complete Newbie Question
  5. Supersedes: <m220oavowh.fsf@hawk.no>
  6. Date: 04 Feb 1996 21:26:48 +0100
  7. Organization: LH*281263
  8. Message-ID: <m2zqayu99z.fsf_-_@hawk.no>
  9. References: <4f0ibf$2pe@mars.dsu.edu>
  10. NNTP-Posting-Host: nm6-ppp14.oslo.net
  11. In-reply-to: scotty@fpg.gcomm.com's message of 3 Feb 1996 20:59:27 GMT
  12. X-Newsreader: Gnus v5.0.12
  13.  
  14. Scotty writes:
  15.  
  16.  : I'm just learning C, and for the life of me I can't find anywhere
  17.  : how to make the printf statements print in the Upper left corner of the
  18.  : screen, nor how to clear the screen.  If you could help me, please do! ;)
  19.  
  20. Here is a `hack' using escape sequences to do the screen stuff Your
  21. asking for. But, if Your on Unix consider to use ncurses instead, on
  22. dos something similar .
  23.  
  24. The following are from an excellent article by Matt Weisfeld in one of
  25. last Years DDJ:
  26.  
  27. jhh
  28.  
  29. -----<snip>------------------------------------------------------------------
  30. /***************************************************
  31.   
  32.   FILE NAME   : scrlibs.h                         
  33.   AUTHOR      : Matt Weisfeld                     
  34.                                                   
  35.   DESCRIPTION : header file for scrlibs.c         
  36.                                                   
  37. ***************************************************/
  38. #ifdef VMS
  39. #define ANSI_COMPILER
  40. #define ANSI_SEQUENCES
  41. #define VT100
  42. #endif
  43.  
  44. #ifdef SLC
  45. #define UNIX
  46. #define K_R_COMPILER
  47. #define ANSI_SEQUENCES
  48. #define VT100
  49. #endif
  50.  
  51. #ifdef BCC
  52. #define DOS
  53. #define ANSI_COMPILER
  54. #define ANSI_SEQUENCES
  55. #endif
  56.  
  57. #ifdef HPUX
  58. #define UNIX
  59. #define ANSI_COMPILER
  60. #define HPUX_SEQUENCES
  61. #endif
  62.  
  63. #ifdef ANSI_COMPILER
  64.  
  65. void do_bold(FILE *);
  66. void do_normal(FILE *);
  67. void do_blink(FILE *);
  68. void do_reverse(FILE *);
  69.  
  70. void cursor_right(int);
  71. void cursor_left(int);
  72. void cursor_down(int);
  73. void cursor_up(int);
  74. void cursor_pos(int, int);
  75. void cursor_home(void);
  76.  
  77. void erase_display(void);
  78.  
  79. #else
  80.  
  81. void do_bold();
  82. void do_normal();
  83. void do_blink();
  84. void do_reverse();
  85.  
  86. void cursor_right();
  87. void cursor_left();
  88. void cursor_down();
  89. void cursor_up();
  90. void cursor_pos();
  91. void cursor_home();
  92. void cursor_bottom();
  93.  
  94. void erase_display();
  95.  
  96. #endif
  97.  
  98. #ifdef ANSI_SEQUENCES
  99.  
  100. #ifdef DOS
  101. static char bold[]      = {"\033[36;1m"};
  102. #else
  103. static char bold[]      = {"\033[1m"};
  104. #endif
  105. static char normal[]    = {"\033[0m"};
  106. static char blink[]     = {"\033[5m"};
  107. static char reverse[]   = {"\033[7m"};
  108.  
  109. static char curright[]  = {"\033[%dC"};
  110. static char curleft[]   = {"\033[%dD"};
  111. static char curdown[]   = {"\033[%dB"};
  112. static char curup[]     = {"\033[%dA"};
  113. static char curpos[]    = {"\033[%d;%dH"};
  114. static char erasedisp[] = {"\033[2J"};
  115.  
  116. #endif
  117.  
  118. #ifdef HPUX_SEQUENCES
  119.  
  120. static char bold[]      = {"\033&dH"};
  121. static char normal[]    = {"\033&d@"};
  122. static char blink[]     = {"\033&dA"};
  123. static char reverse[]   = {"\033&dK"};
  124. static char underline[] = {"\033&dD"};
  125.  
  126. static char curfor[]    = {"\033&a+%dC"};
  127. static char curback[]   = {"\033&a-%dC"};
  128. static char curdown[]   = {"\033&a+%dR"};
  129. static char curup[]     = {"\033&a-%dR"};
  130. static char curpos[]    = {"\033&a%dx%dY"};
  131.  
  132. static char erasedisp[] = {"\033J"};
  133.  
  134. #endif
  135.  
  136. -----<snip>------------------------------------------------------------------
  137. /***************************************************
  138.                                                   
  139.   FILE NAME   : scrlibs.c                         
  140.   AUTHOR      : Matt Weisfeld                     
  141.                                                   
  142.   DESCRIPTION : screen handling libraries         
  143.                                                   
  144. ***************************************************/
  145. #include <stdio.h>
  146. #include "scrlibs.h"
  147.  
  148. void do_bold(stream)
  149. FILE *stream;
  150. {
  151.         fprintf (stream, bold);
  152.         fflush(stream);
  153.         return;
  154. }
  155.  
  156. void do_normal(stream)
  157. FILE *stream;
  158. {
  159.         fprintf (stream, normal);
  160.         fflush(stream);
  161.         return;
  162. }
  163.  
  164. void do_blink(stream)
  165. FILE *stream;
  166. {
  167.         fprintf (stream, blink);
  168.         fflush(stdout);
  169.         return;
  170. }
  171.  
  172. void do_reverse(stream)
  173. FILE *stream;
  174. {
  175.         fprintf (stream, reverse);
  176.         fflush(stdout);
  177.         return;
  178. }
  179.  
  180. void cursor_right(move)
  181. int move;
  182. {
  183.         int i;
  184.  
  185.         printf (curright, move);
  186.         fflush(stdout);
  187.         return;
  188. }
  189.  
  190. void cursor_left(move)
  191. int move;
  192. {
  193.         int i;
  194.  
  195.         printf (curleft, move);
  196.         fflush(stdout);
  197.         return;
  198. }
  199.  
  200. void cursor_down(move)
  201. int move;
  202. {
  203.         int i;
  204.  
  205.         printf (curdown, move);
  206.         fflush(stdout);
  207.         return;
  208. }
  209.  
  210. void cursor_up(move)
  211. int move;
  212. {
  213.         int i;
  214.  
  215.         printf (curup, move);
  216.  
  217.         fflush(stdout);
  218.         return;
  219. }
  220.  
  221. void cursor_pos(row,col)
  222. int row,col;
  223. {
  224.  
  225. #ifdef HPUX
  226.         printf (curpos, col,row);
  227. #else
  228.         printf (curpos, row,col);
  229. #endif
  230.         fflush(stdout);
  231.         return;
  232. }
  233.  
  234. void cursor_home()
  235. {
  236.         cursor_pos(0,0);
  237.         fflush(stdout);
  238.         return;
  239. }
  240.  
  241. void cursor_bottom()
  242. {
  243.         cursor_pos(23,0);
  244.         fflush(stdout);
  245.         return;
  246. }
  247.  
  248. void erase_display()
  249. {
  250.         int i;
  251.  
  252.         cursor_home();
  253.         printf (erasedisp);
  254.         fflush(stdout);
  255.         return;
  256. }
  257.  
  258.  
  259. --<snip>----------------------------------------------------------------------
  260. /* 
  261.  * My own Homemade KeyPressed routine: Wait for a User to press a key.
  262.  */
  263. void KeyPressed ()
  264. {
  265.   int c;
  266.   struct termio tio, tin;
  267.  
  268.   ioctl (0, TCGETA, &tio);
  269.   
  270.   tin = tio;
  271.   tin.c_lflag &= ~ECHO;   /* echo off  */
  272.   tin.c_lflag &= ~ICANON; /* ICANON off */
  273.   /*
  274.    * Emulate cbreak mode
  275.    */
  276.   tin.c_cc[VMIN] = 1;
  277.   tin.c_cc[VTIME] = 0;
  278.   
  279.   /* 
  280.    * Set cbreak-mode
  281.    */
  282.   ioctl (0, TCSETA, &tin);
  283.  
  284.   /* Read a char and reset the tty */
  285.   c = getchar ();
  286.  
  287.   ioctl(0, TCSETA, &tio);
  288.  
  289. /* A alternative for lazy programmers                      */
  290. /*      system ("/bin/stty cbreak");                       */
  291. /*      c = getchar ();                                    */
  292. /*      system ("/bin/stty -cbreak");                      */
  293.   return;
  294. }
  295.